<?php
// +----------------------------------------------------------------------
// | ebSIG
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.ebsig.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: liudaojian <liudaojian@ebsig.com>
// +----------------------------------------------------------------------

/**
 * 商品列表服务
 * @author   liudaojian <liudaojian@ebsig.com>
 * @version 1.0
 */

namespace App\Service\Goods;

use DB;

use Illuminate\Support\Facades\Redis as Redis;

class GoodsListService
{

    public static function searchCache($platform, $keyword, $mall_id, $sort, $order, $page, $limit)
    {

        if (empty($platform) || !in_array($platform, ['wap', 'pc', 'app'])) {
            return [];
        }
        if (empty($keyword)) {
            return [];
        }

        $keyword_hash = md5($keyword);

        /**
         * 保存商品搜索列表的redis key名称
         * 规则：WDH_GOODS_LIST_平台ID_关键词hash_排序字段_排序方式_门店ID
         * WDH_GOODS_LIST_wap_8f56f054ba4311e7b3276c92bf21bb4d_update_desc_10001
         */
        $redis_key = 'WDH_GOODS_LIST_' . $platform . '_ ' . $keyword_hash . '_' . $sort . '_' . $order . '_' . $mall_id;
        $goods_count = Redis::zCard($redis_key); //检查商品列表数量
        if ($goods_count == 0) {

            $goods_sql = ' gm.goodsName LIKE ? AND gm.forsale = 1 AND gm.useFlg = 1';
            $params[] = '%' . $keyword . '%';

            //平台
            switch ($platform) {
                case 'pc':
                    $goods_sql .= ' AND gm.PC = 1';
                    break;
                case 'wap':
                    $goods_sql .= ' AND gm.WAP = 1';
                    break;
                case 'app':
                    $goods_sql .= ' AND gm.APP = 1';
                    break;
                default:
                    break;
            }

            if (!empty($sort)) {
                switch ($sort) {
                    case 'price':
                        $sort = 'salePrice';
                        break;
                    case 'update':
                        $sort = 'timeStamp';
                        break;
                    case 'discount':
                        $sort = 'discount';
                        break;
                    case 'sale':
                        $sort = 'saletotal';
                        break;
                    default:
                        $sort = 'timeStamp';
                        break;
                }
            }
            if (!in_array($order, ['desc', 'asc'])) {
                return [];
            }
            $goods_sql .= sprintf(' ORDER BY gm.%s %s', $sort, $order);

            //查询商品
            $search_sql = 'SELECT gm.postID FROM goods_master gm
                           INNER JOIN wms_goods_master wm ON wm.storeID = ' . $mall_id . ' AND wm.mainPostID = gm.mainPostID 
                           WHERE ' . $goods_sql;
            $goods_list = DB::select($search_sql, $params);
            if ($goods_list) {
                foreach ($goods_list as &$goods) {
                    Redis::zAdd($redis_key, $goods_count, $goods['postID']);
                    $goods_count++;
                }
                Redis::expire($redis_key, 7200); //设置过期时间为2小时
            }

        }

        if ($goods_count == 0) {
            return [];
        }

        //从小到大分页获取商品网购号
        $goods_list = Redis::zRange($redis_key, ($page-1) * $limit, $page * $limit - 1);
        if (!$goods_list) {
            return [];
        }

        return $goods_list;

    }

    /**
     * 商品列表缓存商品网购号
     * @param string $platform 平台[wap、pc、app]
     * @param int $big_id 大分类ID
     * @param int $mid_id 中分类ID
     * @param int $small_id 小分类ID
     * @param int $mall_id 门店id
     * @param int|string $brand 品牌id，多个id以“_”隔开
     * @param string $sort 排序字段【price、update、discount、sale】
     * @param string $order 排序方式【desc, asc】
     * @param int $page 页码
     * @param int $limit 每页显示条数
     * @return array
     */
    public static function listCache($platform, $big_id, $mid_id, $small_id, $mall_id, $brand, $sort, $order, $page, $limit)
    {

        /**
         * 保存商品列表的redis key名称
         * 规则：WDH_GOODS_LIST_平台ID_大分类ID_中分类ID_小分类ID_品牌ID_排序字段_排序方式_门店ID
         * WDH_GOODS_LIST_wap_10_110_500_0_update_desc_10001
         */
        $redis_key = 'WDH_GOODS_LIST_' . $platform . '_' . $big_id . '_ ' . $mid_id . ' _' . $small_id . '_' . $brand . '_' . $sort . '_' . $order . '_' . $mall_id;

        $goods_count = Redis::zCard($redis_key); //检查商品列表数量
        if ($goods_count == 0) {

            //分类sql
            $category_sql = '';
            if (ebsig_is_int($big_id)) {
                $category_sql .= ' AND bigCategoryID = ' . $big_id;
            }
            if (ebsig_is_int($mid_id)) {
                $category_sql .= ' AND midCategoryID = ' . $mid_id;
            }
            if (ebsig_is_int($small_id)) {
                $category_sql .= ' AND smallCategoryID = ' . $small_id;
            }
            if ($category_sql) {
                $category_sql = ' WHERE ' . substr($category_sql, 4);
            }

            //商品主表查询sql
            $goods_sql = ' gm.forsale = 1 AND gm.useFlg = 1';

            //平台
            switch ($platform) {
                case 'pc':
                    $goods_sql .= ' AND gm.PC = 1';
                    break;
                case 'wap':
                    $goods_sql .= ' AND gm.WAP = 1';
                    break;
                case 'app':
                    $goods_sql .= ' AND gm.APP = 1';
                    break;
                default:
                    break;
            }

            //品牌
            if ($brand) {
                $brand_arr = explode('_', $brand);
                if (isset($brand_arr[1])) {
                    $str = '';
                    foreach ( $brand_arr['brand'] as &$brand_id ) {
                        if (ebsig_is_int($brand_id)) {
                            $str .= " OR gm.brandID = $brand_id";
                        }
                    }
                    $str = ltrim(trim($str), 'OR ');
                    $goods_sql .= ' AND ' .'(' . $str . ')';
                } else {
                    if (ebsig_is_int($brand_arr[0])) {
                        $goods_sql .= ' AND gm.brandID = ' . $brand_arr[0];
                    }
                }
            }

            if (!empty($sort)) {
                switch ($sort) {
                    case 'price':
                        $sort = 'salePrice';
                        break;
                    case 'update':
                        $sort = 'timeStamp';
                        break;
                    case 'discount':
                        $sort = 'discount';
                        break;
                    case 'sale':
                        $sort = 'saletotal';
                        break;
                    default:
                        $sort = 'timeStamp';
                        break;
                }
            }
            if (!in_array($order, ['desc', 'asc'])) {
                return [];
            }
            $goods_sql .= sprintf(' ORDER BY gm.%s %s', $sort, $order);

            //查询商品
            $search_sql = 'SELECT gm.postID FROM goods_master gm
                       INNER JOIN ( SELECT DISTINCT mainPostID FROM goods_extend_category ' . $category_sql . ' ) AS gec ON gec.mainPostID = gm.mainPostID
                       INNER JOIN wms_goods_master wm ON wm.storeID = ' . $mall_id . ' wm.mainPostID = gm.mainPostID 
                       WHERE ' . $goods_sql;
            $goods_list = DB::select($search_sql);
            if ($goods_list) {
                foreach ($goods_list as &$goods) {
                    Redis::zAdd($redis_key, $goods_count, $goods['postID']);
                    $goods_count++;
                }
                Redis::expire($redis_key, 7200); //设置过期时间为2小时
            }

        }

        if ($goods_count == 0) {
            return [];
        }

        //从小到大分页获取商品网购号
        $goods_list = Redis::zRange($redis_key, ($page-1) * $limit, $page * $limit - 1);
        if (!$goods_list) {
            return [];
        }

        return $goods_list;

    }

}